
const { contextBridge,ipcRenderer } = require('electron')




const sendUrl = async (url) => {
    let result = await ipcRenderer.invoke('on-url-event',url)
    return result
    
}


const alert = async (msg) => {
    await ipcRenderer.invoke('on-alert-event',msg)
}



const open = (url) => {
    ipcRenderer.invoke('on-open-event',url)
}

const getFilelist = async () => {
    const fileList = await ipcRenderer.invoke('on-getfiles-event')
    return fileList
}

const openDialog = () => {
    ipcRenderer.send('on-opendialog-event')
}

const onRendererEvent = () => {
    // ipcRenderer.on('on-renderer-event',(e,msg) => {
    //     console.log(msg)
    // })


    return new Promise((resolve,reject) => {
        ipcRenderer.on('on-renderer-event',(e,msg) => {
            
            resolve(msg)
        })
    })
}


//方式二
// const onRendererEvent = (cb) => {

//         ipcRenderer.on('on-renderer-event',(e,msg) => {
//             cb()
//         })

// }


contextBridge.exposeInMainWorld('myApi',{
    sendUrl,
    alert,
    open,
    getFilelist,
    openDialog,
    onRendererEvent
})